package com.jinkaimen.kqb.ui.activity.addmerchant;

import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.TextView;

import com.ht.baselibs.retrofit.NetCallBack;
import com.ht.baselibs.ui.base.BaseActivity;
import com.ht.baselibs.utils.ActivityUtils;
import com.ht.baselibs.utils.UIUtils;
import com.jinkaimen.kqb.R;
import com.jinkaimen.kqb.bean.AppChannelRate;
import com.jinkaimen.kqb.bean.SearchShopBean;
import com.jinkaimen.kqb.bean.SearchShopPolicy;
import com.jinkaimen.kqb.manager.ApiManager;
import com.jinkaimen.kqb.ui.weiget.UISwitchButton;

import java.math.BigDecimal;
import java.math.RoundingMode;

import retrofit2.Call;
import retrofit2.Response;

public class OpenServiceActivity extends BaseActivity {

    private UISwitchButton account_switch_weixin;
    private UISwitchButton account_switch_zhifubao;
    private UISwitchButton account_switch_d0;
    private EditText et_rate_weixin;
    private EditText et_rate_zhifubao;
    private boolean isOpenWeixin;
    private boolean isOpenZhifubao;
    private boolean isOpenD0;
    public long mShopId;
    public long mUserId;
    public double zhifubaomin;//最小费率
    public double zhifubaomax;//最大费率
    public double weixinmin;//最小费率
    public double weixinmax;//最大费率
    public TextView btn_activity_submit;
    public String wechatTradeRateT1;
    public String alipayTradeRateT1;

    @Override
    protected boolean isStaticPage() {
        return true;
    }

    @Override
    protected void initTitleBar() {
        mTitleBarView.setTitleText("开通业务");
    }

    @Override
    protected int initContentView() {
        return R.layout.activity_open_service;
    }

    @Override
    protected void initIntentData(Intent callIntent) {
        mShopId = callIntent.getLongExtra("report_shop_id", 0);
        mUserId = callIntent.getLongExtra("report_account_id", 0);
    }

    @Override
    protected void initChildView() {
        account_switch_weixin = (UISwitchButton) findViewById(R.id.account_switch_weixin);
        account_switch_zhifubao = (UISwitchButton) findViewById(R.id.account_switch_zhifubao);
        account_switch_d0 = (UISwitchButton) findViewById(R.id.account_switch_d0);
        et_rate_weixin = (EditText) findViewById(R.id.et_rate_weixin);
        et_rate_zhifubao = (EditText) findViewById(R.id.et_rate_zhifubao);
        btn_activity_submit = (TextView) findViewById(R.id.btn_activity_submit);


    }

    @Override
    protected void initChildData() {

        ApiManager.getApi().searchShopPolicy(
                "INV_HSY001016",
                mUserId
        ).enqueue(new NetCallBack<SearchShopPolicy>() {
            @Override
            protected void onSuccess(Call<SearchShopPolicy> call, Response<SearchShopPolicy> response) {
                SearchShopPolicy searchShopPolicy = response.body();
                if (searchShopPolicy != null) {
                    zhifubaomin = searchShopPolicy.alipayDealerCurrentRatePolicy.merchantMinRateT1;
                    zhifubaomax = searchShopPolicy.alipayDealerCurrentRatePolicy.merchantMaxRateT1;
                    weixinmin = searchShopPolicy.wechatDealerCurrentRatePolicy.merchantMinRateT1;
                    weixinmax = searchShopPolicy.wechatDealerCurrentRatePolicy.merchantMaxRateT1;
                }
                showLoadingDialog();
                ApiManager.getApi().searchShopInfo(
                        "INV_HSY001010",
                        mUserId
                ).enqueue(new NetCallBack<SearchShopBean>() {
                    @Override
                    protected void onSuccess(Call<SearchShopBean> call, Response<SearchShopBean> response) {
                        AppChannelRate appChannelRate = response.body().appChannelRate;
                        isOpenZhifubao = appChannelRate.alipayIsOpen;
                        isOpenWeixin = appChannelRate.wechatIsOpen;
                        isOpenD0 = appChannelRate.isOpenD0;
                        wechatTradeRateT1 = appChannelRate.wechatTradeRateT1;
                        alipayTradeRateT1 = appChannelRate.alipayTradeRateT1;
                        account_switch_weixin.setChecked(isOpenWeixin);
                        account_switch_zhifubao.setChecked(isOpenZhifubao);
                        account_switch_d0.setChecked(isOpenD0);
                        if (!TextUtils.isEmpty(wechatTradeRateT1)) {
                            String weixin = new BigDecimal(wechatTradeRateT1).multiply(new BigDecimal("100")) + "";
                            et_rate_weixin.setText(getTwoNum(weixin));
                        }
                        if (!TextUtils.isEmpty(alipayTradeRateT1)) {
                            String zhifubao = new BigDecimal(alipayTradeRateT1).multiply(new BigDecimal("100")) + "";
                            et_rate_zhifubao.setText(getTwoNum(zhifubao));
                        }
                        startInfo1();
                        startInfo2();

                    }

                    @Override
                    protected boolean isNullNeedCallback() {
                        return true;
                    }

                    @Override
                    protected void hiddenProgress() {
                        hideLodingDialog();
                    }
                });
            }

            @Override
            protected boolean isNullNeedCallback() {
                return true;
            }

            @Override
            protected void hiddenProgress() {
                hideLodingDialog();
            }
        });
    }

    private String getTwoNum(String num) {

        if (!TextUtils.isEmpty(num) && num.length() > 4) {
            num = num.substring(0, 4);
        }
        return num;

    }

    @Override
    protected void setChildViewListener() {
        btn_activity_submit.setOnClickListener(this);
        account_switch_weixin.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton arg0, boolean check) {
                isOpenWeixin = check;
                startInfo1();
            }
        });
        account_switch_zhifubao.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton arg0, boolean check) {
                isOpenZhifubao = check;
                startInfo2();
            }
        });

        account_switch_d0.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton arg0, boolean check) {
                isOpenD0 = check;
            }
        });
    }

    private void startInfo2() {
        if (isOpenZhifubao) {
            et_rate_zhifubao.setFocusable(true);
            et_rate_zhifubao.setFocusableInTouchMode(true);
            account_switch_d0.setEnabled(true);
        } else {
            try {
                ((InputMethodManager) getSystemService(INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(OpenServiceActivity.this.getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
            } catch (Exception e) {
                e.printStackTrace();
            }
            et_rate_zhifubao.setFocusable(false);
            et_rate_zhifubao.setFocusableInTouchMode(false);
            if (!isOpenWeixin) {
                account_switch_d0.setEnabled(false);
            }
        }
    }

    private void startInfo1() {
        if (isOpenWeixin) {
            et_rate_weixin.setFocusable(true);
            et_rate_weixin.setFocusableInTouchMode(true);
            account_switch_d0.setEnabled(true);
        } else {
            try {
                ((InputMethodManager) getSystemService(INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(OpenServiceActivity.this.getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
            } catch (Exception e) {
                e.printStackTrace();
            }
            et_rate_weixin.setFocusable(false);
            et_rate_weixin.setFocusableInTouchMode(false);
            if (!isOpenZhifubao) {
                account_switch_d0.setEnabled(false);
            }
        }
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.btn_activity_submit:
                wechatTradeRateT1 = et_rate_weixin.getText().toString().trim();
                alipayTradeRateT1 = et_rate_zhifubao.getText().toString().trim();
                if (isOpenWeixin) {
                    if (TextUtils.isEmpty(wechatTradeRateT1)) {
                        UIUtils.showToast("请输入微信费率");
                        return;
                    } else {
                        double weixin = Double.parseDouble(wechatTradeRateT1);
                        if (weixin > (weixinmax * 100)) {
                            UIUtils.showToast("微信费率最大为" + getTwoNum(new BigDecimal(weixinmax).multiply(new BigDecimal("100")) + "") + "％,请重新设置");
                            return;
                        } else if (weixin < (weixinmin * 100)) {
                            UIUtils.showToast("微信费率最小为" + getTwoNum(new BigDecimal(weixinmin).multiply(new BigDecimal("100")) + "") + "％,请重新设置");
                            return;
                        }
                    }
                }

                if (isOpenZhifubao) {
                    if (TextUtils.isEmpty(alipayTradeRateT1)) {
                        UIUtils.showToast("请输入微信费率");
                        return;
                    } else {
                        double zhifubao = Double.parseDouble(alipayTradeRateT1);
                        if (zhifubao > (zhifubaomax * 100)) {
                            UIUtils.showToast("支付宝费率最大为" + getTwoNum(new BigDecimal(zhifubaomax).multiply(new BigDecimal("100")) + "") + "％,请重新设置");
                            return;
                        } else if (zhifubao < (zhifubaomin * 100)) {
                            UIUtils.showToast("支付宝费率最小为" + getTwoNum(new BigDecimal(zhifubaomin).multiply(new BigDecimal("100")) + "") + "％,请重新设置");
                            return;
                        }
                    }
                }
                String one = isOpenWeixin ? "1" : "0";
                String two = isOpenZhifubao ? "1" : "0";
                String three = isOpenD0 ? "1" : "0";
                wechatTradeRateT1 = TextUtils.isEmpty(wechatTradeRateT1) ? "0.0000" : wechatTradeRateT1;
                alipayTradeRateT1 = TextUtils.isEmpty(alipayTradeRateT1) ? "0.0000" : alipayTradeRateT1;
                try {
                    wechatTradeRateT1 = new BigDecimal(wechatTradeRateT1).divide(new BigDecimal("100"), 4, RoundingMode.HALF_UP) + "";
                    alipayTradeRateT1 = new BigDecimal(alipayTradeRateT1).divide(new BigDecimal("100"), 4, RoundingMode.HALF_UP) + "";
                } catch (Exception e) {
                    e.printStackTrace();
                }
                ApiManager.getApi().submitPolicy(
                        "INV_HSY001017",
                        mUserId,
                        wechatTradeRateT1,
                        alipayTradeRateT1,
                        one,
                        two,
                        three
                ).enqueue(new NetCallBack<String>() {
                    @Override
                    protected void onSuccess(Call<String> call, Response<String> response) {
                        UIUtils.showToast("提交成功");
                        Intent intent = new Intent();
                        intent.putExtra("isOpenWeixin", isOpenWeixin);
                        intent.putExtra("isOpenZhifubao", isOpenZhifubao);
                        intent.putExtra("isOpenD0", isOpenD0);
                        intent.putExtra("wechatTradeRateT1", wechatTradeRateT1);
                        intent.putExtra("alipayTradeRateT1", alipayTradeRateT1);
                        setResult(RESULT_OK, intent);
                        ActivityUtils.finishActivity(OpenServiceActivity.this);
                    }
                });
                break;
        }
    }


}
